package b3.widget.guild
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.scene.*;
	import b3.scene.build.*;
	import b3.util.*;
	import b3.widget.*;
	import b3.widget.basic.*;
	import b3.widget.tab.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.net.*;
	import flash.text.*;
	
	public class GuildInfoTab extends Tab
	{
		public var guildArray:Array;
		public var memberArray:Array;
		public var guildInfoArray:Array;
		public var requestArray:Array;
		
		public function GuildInfoTab()
		{
		
			super("公会",1,5);
			
			var TabPageFirstPage:Class = ResourceHelper.getInstance().getClassByName("TabPageFirstPage3");
			var TabPagePageUp:Class = ResourceHelper.getInstance().getClassByName("TabPagePageUp3");
			var TabPagePageDown:Class = ResourceHelper.getInstance().getClassByName("TabPagePageDown3");
			var TabPageLastPage:Class = ResourceHelper.getInstance().getClassByName("TabPageLastPage3");
			setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0x66FF, 0x66FF);
			//设置页码位置
			this.setPagePos(0, 0);
			this.setPageSelectorPos(207, 278);
			// 更新 信息
			this.getGuildInfo();
			
		}
		
		public function getGuildInfo(force:Boolean = false):void
		{
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.GET_GUILD_INFO, {userId:PlayerManager.getInstance().myUserId});
			serverRequest.setHandleFunc(handleLoadGuildInfo);
			serverRequest.post(true);
		}
		
		public function handleLoadGuildInfo(data:Object):void
		{
			var guildFlag:Boolean = data['guild'];
			if (guildFlag)
			{//如果已经是公会成员
				var memberList:Object = data['memberList'];
				memberArray = new Array();
				for (var key:String in memberList)
				{
					var guildMember:GuildMember = new GuildMember( memberList[key]['userId'],memberList[key]['userName']);
					memberArray.push( guildMember );
				}
				var requestList:Object = data['requestList'];
				requestArray = new Array();
				for (var key:String in requestList)
				{
					var guildMember:GuildMember = new GuildMember( requestList[key]['requestId'], requestList[key]['requestName']);
					requestArray.push( guildMember );
				}
				var priority:int = data['priority'];
				showMemberList(priority);
			}
			else
			{//不是公会成员
				var guildList:Object = data['guildList'];
				guildArray = new Array();
				for (var key:String in guildList)
				{
					var guild:Guild = new Guild(guildList[key]['guildId'],guildList[key]['guildName']);
					guildArray.push( guild );
				}
				showGuildList();
			}
		}
		
		public function showMemberList( priority:int):void
		{//显示成员栏目
			//成员列表
			for (var i:int = 0; i < memberArray.length; i++)
			{
				var memberItem:GuildMemberItem = new GuildMemberItem(memberArray[i], true, priority);
				addItem(memberItem, memberItem.width, memberItem.height);
			}
			for (i = 0; i < requestArray.length; i++)
			{
				memberItem = new GuildMemberItem(requestArray[i], false, priority);
				addItem(memberItem, memberItem.width, memberItem.height);
			}
			//退出公会
			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr1");
			var buttonQuit:BasicButton = new BasicButton(ButtonBgr, "退出公会");
			buttonQuit.addEventListener(MouseEvent.CLICK, onButtonQuitClick);
			buttonQuit.x = 270;
			buttonQuit.y = 200;
			addChild(buttonQuit);
		}
		
		private function onButtonQuitClick(event:MouseEvent):void
		{//点击退出公会
			var messageBox:MessageBox = new MessageBox("退出公会", true);
			messageBox.x = -messageBox.width / 2;
			messageBox.y = -messageBox.height / 2;
			messageBox.setOKCall(onQuitConfirmClick);
			WidgetManager.getInstance().popOut([messageBox], true);
		}
		
		private function onQuitConfirmClick():void
		{
			GuildManager.getInstance().quitGuild();	
		}
		
		public function showGuildList():void
		{//显示公会列表
			//公会列表
			for (var i:int = 0; i < guildArray.length; i++)
			{
				var guildItem:GuildInfoItem = new GuildInfoItem(guildArray[i]);
				addItem(guildItem, guildItem.width, guildItem.height);
			}
			//创建公会
			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr1");
			var buttonCreate:BasicButton = new BasicButton(ButtonBgr, "创建公会");
			buttonCreate.addEventListener(MouseEvent.CLICK, onButtonCreateClick);
			buttonCreate.x = 270;
			buttonCreate.y = 200;
			addChild(buttonCreate);
		}
		
		public function onButtonCreateClick(event:MouseEvent):void
		{//点击开始创建公会
			var createBox:GuildCreateDialog = new GuildCreateDialog();
			createBox.x = -createBox.width / 2;
			createBox.y = -createBox.height / 2;
			guildInfoArray = new Array();
			createBox.setOKCall(onCreateConfirmClick,guildInfoArray);
			WidgetManager.getInstance().popOut([createBox], true);
			
		}
		
		private function onCreateConfirmClick(guildName:String):void
		{
			var guildName:String = guildInfoArray[0];
			//获得公会名称 调用模型创建工会
			GuildManager.getInstance().createGuild(guildName);
			
			
		}

	}
}